#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include	<termios.h> 
#include "include/obd_uart.h"

#define FALSE 0
#define TRUE  1

struct termios old_options;
struct termios gps_options;

char *dev ="/dev/ttyS0",*dev1 ="/dev/ttyS1",*dev2 ="/dev/ttyS2",*dev3 ="/dev/ttyS3";


/***@brief  set serial com baudrate
*@param  fd     type int  the file handle of the opened serial port
*@param  speed  type int  the serial port speed
*@return  void*/

int speed_arr[5] = { B115200, B57600, B38400, B19200, B9600};

int name_arr[5] = {115200, 57600, 38400,  19200,  9600};

int reg_arr[5] = {0x23, 0x45, 0x68, 0xD0, 0x68};
void set_speed(int fd, int speed)
{
  int   i;
  int   status;
  struct termios   Opt;
  tcgetattr(fd, &Opt);
  
  for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
   {
   	if  (speed == name_arr[i])
   	{
   	    tcflush(fd, TCIOFLUSH);
    	cfsetispeed(&Opt, speed_arr[i]);
    	cfsetospeed(&Opt, speed_arr[i]);
    	status = tcsetattr(fd, TCSANOW, &Opt);
    	if  (status != 0)
            perror("tcsetattr fd1");
     	return;
     	}
   tcflush(fd,TCIOFLUSH);
   }
}
/**
*@brief   Set data bit,stop bit and parity bit
*@param  fd     type  int  the file handle of the opened serial port
*@param  databits type  int data bits    value: 7 or 8
*@param  stopbits type  int stop bit     value: 1 or 2
*@param  parity  type  int  check bit value:N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
	struct termios options;
 if  ( tcgetattr( fd,&options)  !=  0)
  {
  	perror("SetupSerial 1");
  	return(FALSE);
  }
  options.c_cflag &= ~CSIZE;
  switch (databits) /*Set the bits of the data*/
  {
  	case 7:
  		options.c_cflag |= CS7;
  		break;
  	case 8:
		options.c_cflag |= CS8;
		break;
	default:
		fprintf(stderr,"Unsupported data size\n");
		return (FALSE);
	}
  switch (parity)
  	{
  	case 'n':
	case 'N':
		options.c_cflag &= ~PARENB;   /* Clear parity enable */
		options.c_iflag &= ~INPCK;     /* Enable parity checking */
		break;
	case 'o':
	case 'O':
		options.c_cflag |= (PARODD | PARENB);  /* Set the parity bit*/ 
		options.c_iflag |= INPCK;             /* Disnable parity checking */
		break;
	case 'e':
	case 'E':
		options.c_cflag |= PARENB;     /* Enable parity */
		options.c_cflag &= ~PARODD;   /* change to EVEN parity*/  
		options.c_iflag |= INPCK;       /* Disnable parity checking */
		break;
	case 'S':
	case 's':  /*as no parity*/
		options.c_cflag &= ~PARENB;
		options.c_cflag &= ~CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported parity\n");
		return (FALSE);
		}
  /* Set stop bit*/   
  switch (stopbits)
  	{
  	case 1:
  		options.c_cflag &= ~CSTOPB;
		break;
	case 2:
		options.c_cflag |= CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported stop bits\n");
		return (FALSE);
	}
	
	options.c_cflag &= ~CRTSCTS;	
  /* Set input parity option */
  if (parity != 'n')
  		options.c_iflag |= INPCK;
    options.c_cc[VTIME] = 150; // 15 seconds
    options.c_cc[VMIN] = 0;

  options.c_lflag  &= ~(ICANON | ECHO) ;
  
  tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  if (tcsetattr(fd,TCSANOW,&options) != 0)
  	{
  		perror("SetupSerial 3");
		return (FALSE);
	}
  return (TRUE);
 }
/**
*@breif Open the serial port
*/
int OpenDev(char *Dev)
{
int	fd = open( Dev, O_RDWR | O_NONBLOCK);         //| O_NOCTTY | O_NDELAY
	if (-1 == fd)
		{ /*Set the Data bits*/
			perror("Can't Open Serial Port");
			return -1;
		}
	else
	return fd;

}

